/*
package com.ruige.account.filter;


import com.ruige.account.service.AuthService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
@Order(-1)
public class AuthFilter implements Filter {

    public static final String LOGIN_URL = "/api/auth/oauth/toLogin";
    public static final String Authorization = "Authorization";
    public static final Logger log = LoggerFactory.getLogger(AuthFilter.class);

    @Autowired
    private AuthService authService;

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String requestURI = request.getRequestURI();
        log.info(requestURI + "进入过滤器!");
        System.out.println("requestURI = " + requestURI);
        HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper(request) {
            @Override
            public String getHeader(String name) {
                // 先从原本的Request中获取头，如果为空且名字为token，则从参数中查找并返回
                String superHeader = super.getHeader(name);
                if ("Authorization".equals(name) && StringUtils.isEmpty(superHeader)) {
                    String token = "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6WyJhcHAiXSwibmFtZSI6Iuedv-aIiCIsImlkIjoiMSIsImV4cCI6MTU5MzEwNDA0NiwiYXV0aG9yaXRpZXMiOlsic2Vja2lsbF9saXN0Iiwic2FsZXNtYW4iLCJnb29kc19saXN0IiwiYWNjb3VudGFudCIsInVzZXIiXSwianRpIjoiYjc5YWY2NjMtODI5OC00ODZiLWFjZTctOTNkYzRlN2U5ZGEwIiwiY2xpZW50X2lkIjoicnVpZ2VibG9nIiwidXNlcm5hbWUiOiJydWlnZSJ9.lrZ0KsZYqYDGXCS1vCPc_gm4y9_3dGInMem8ODGNUlFV_gwLl-K2bj-HV5vVGMTLqlqNjZ2wThoByPR04aLkgtfClYz4Tsj4W8uvIOQBPvoR42XBGUiI13FgQsM7P6UylFvo1HfuGhjiO3fKn2WeOlFoG0CdSniwJZygJrSPoZybHYKt3GsBdcJ85VCbcnGo9TH4u5HIwVc0gDtkFJmwB8jA_93y1Nqh4pfdaXh-MM8zL1M9r2PPAqiTUOAYeQUYj6iiTmlWfiuLwyytS7rbsuB9h6E21Z8JWWFDeNJOpvAz_XxW_6SS6xppubQ7uAKjgwHlQIED59_JdXpQA12zqg";
//                    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken());
                    return token;
                }
                return superHeader;
            }
        };

        String auth = requestWrapper.getHeader("Authorization");
        System.out.println("Authorization = " + auth);
        log.info("请求增强并放行");
        filterChain.doFilter(requestWrapper, response);


    }

*/
/*    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        //获取当前请求对象
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();

        log.info(request.getURI().getPath() + "进入过滤器!");

        String path = request.getURI().getPath();
        if ("/api/auth/oauth/login".equals(path) || "/api/auth/oauth/toLogin".equals(path) || !URLFilter.hasAuthorize(path)) {
            //放行
            log.info("不需要增强,放行");
            return chain.filter(exchange);
        }

        //判断cookie上是否存在jti
        String jti = authService.getJtiFromCookie(request);
        if (StringUtils.isEmpty(jti)) {
            log.info("请求中jti不存在");
            //拒绝访问,请求跳转
            //response.setStatusCode(HttpStatus.UNAUTHORIZED);
            //return response.setComplete();
            return this.toLoginPage(LOGIN_URL + "?FROM=" + request.getURI().getPath(), exchange);

        }

        //判断redis中token是否存在
        String redisToken = authService.getTokenFromRedis(jti);
        if (StringUtils.isEmpty(redisToken)) {
            //拒绝访问,请求跳转
            //response.setStatusCode(HttpStatus.UNAUTHORIZED);
            //return response.setComplete();
            return this.toLoginPage(LOGIN_URL, exchange);
        }

        //校验通过 , 请求头增强,放行
        request.mutate().header(Authorization, "Bearer " + redisToken);
        log.info("请求增强并放行");
        return chain.filter(exchange);
    }

    private Mono<Void> toLoginPage(String loginUrl, ServerWebExchange exchange) {
        log.info("去登录界面!");
        ServerHttpResponse response = exchange.getResponse();
        response.setStatusCode(HttpStatus.SEE_OTHER);
        response.getHeaders().set("location", loginUrl);
        return response.setComplete();
    }*//*


}*/
